﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class RegularExpressions : MissionPlugin
    {
        [Package(Direction.Input, "Defines which data you like to extract.")]
        enum Data2Regex { }

        [Package(Direction.Input, "Defines the rule-set for extracting the given data.")]
        enum RegexRules
        {
            [Column(typeof(string), "Name of the column in which you like work with regular expression.")]
            ColumnName,
            [Column(typeof(string), "The regular expression itself.")]
            RegularExpression
        }

        [Package(Direction.Output, "Defines the extract of the given data.")]
        enum Output
        {
            [Column(typeof(string), "Name of the column in which you like extract data.")]
            ColumnName,
            [Column(typeof(string), "The found value which match the regular expression.")]
            Match
        }

        [Plugin(PluginDevStatus.Production, "Use regular expression on your favorite data columns.")]
        public RegularExpressions()
        {
        }

        public override void Initialize()
        {

            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        /// <summary>
        /// Generate new Source-content.
        /// </summary>
        protected override void Work()
        {
            DataTableReader dataToRegexPackage = this.GetDataReader(typeof(Data2Regex));
            while (dataToRegexPackage.Read())
            {
                DataTableReader regexRulesPackage = this.GetDataReader(typeof(RegexRules));
                while (regexRulesPackage.Read())
                {
                    string columnOn = regexRulesPackage[RegexRules.ColumnName.ToString()].ToString();
                    string regularExpressionOn = regexRulesPackage[RegexRules.RegularExpression.ToString()].ToString();

                    this.AddNewLoggingMessage(typeof(RegexRules), String.Format("start regular expression '{0}' on '{1}'", regularExpressionOn, columnOn));

                    Regex regularExpression = new Regex(regularExpressionOn, RegexOptions.Multiline);
                    Match regularMatch = regularExpression.Match(dataToRegexPackage[columnOn].ToString());
                    while (regularMatch.Success)
                    {
                        DataRow newRow = this.CreateNewRowFromPackage(typeof(Output));
                        newRow[Output.ColumnName.ToString()] = columnOn;
                        newRow[Output.Match.ToString()] = regularMatch.Groups[1].Captures[0].Value;
                        this.AddNewRowToPackage(typeof(Output), newRow);

                        regularMatch = regularMatch.NextMatch();
                    }
                }
            }
        }
    }
}
